/*
 * @lc app=leetcode.cn id=3 lang=javascript
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
    // 滑动窗口 O(n)
    let window = new Set()
    let right = 0
    let maxlen = 0
    for (let left = 0; left < s.length; left++) {
        while (right < s.length && !window.has(s[right])) {
            window.add(s[right])
            right++
        }
        if ((right - left) > maxlen) maxlen = right - left
        window.delete(s[left])
    }
    return maxlen

};
// @lc code=end